package com.xmtlxx.web.controller.sacw;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.xmtlxx.common.annotation.Log;
import com.xmtlxx.common.annotation.RepeatSubmit;
import com.xmtlxx.common.core.controller.BaseController;
import com.xmtlxx.common.core.domain.PageQuery;
import com.xmtlxx.common.core.domain.R;
import com.xmtlxx.common.core.page.TableDataInfo;
import com.xmtlxx.common.core.validate.AddGroup;
import com.xmtlxx.common.core.validate.EditGroup;
import com.xmtlxx.common.core.validate.QueryGroup;
import com.xmtlxx.common.enums.BusinessType;
import com.xmtlxx.common.utils.poi.ExcelUtil;
import com.xmtlxx.sacw.domain.EvaluateInfo;
import com.xmtlxx.sacw.domain.vo.EvaluateInfoVo;
import com.xmtlxx.sacw.service.IEvaluateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 评估Controller
 *
 * @author xmtlxx
 * @date 2022-08-15
 */
@Validated
@Api(value = "评估控制器", tags = {"评估管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/sacw/evaluate")
public class EvaluateController extends BaseController {

    private final IEvaluateService iEvaluateService;

    /**
     * 查询评估列表
     */
    @ApiOperation("查询评估列表")
    @SaCheckPermission("sacw:evaluate:list")
    @GetMapping("/list")
    public TableDataInfo<EvaluateInfoVo> list(@Validated(QueryGroup.class) EvaluateInfo bo, PageQuery pageQuery) {

        return iEvaluateService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出评估列表
     */
    @ApiOperation("导出评估列表")
    @SaCheckPermission("sacw:evaluate:export")
    @Log(title = "评估", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated EvaluateInfo bo, HttpServletResponse response) {

        List<EvaluateInfoVo> list = iEvaluateService.queryList(bo);
        ExcelUtil.exportExcel(list, "评估", EvaluateInfoVo.class, response);
    }

    /**
     * 获取评估详细信息
     */
    @ApiOperation("获取评估详细信息")
    @SaCheckPermission("sacw:evaluate:query")
    @GetMapping("/{id}")
    public R<EvaluateInfo> getInfo(@ApiParam("主键")
                                   @NotNull(message = "主键不能为空")
                                   @PathVariable("id") String id) {

        return R.ok(iEvaluateService.getInfo(id));
    }

    /**
     * 新增评估
     */
    @ApiOperation("新增评估")
    @SaCheckPermission("sacw:evaluate:add")
    @Log(title = "评估", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    @Transactional(rollbackFor = Exception.class)
    public R<Void> add(@Validated(AddGroup.class) @RequestBody EvaluateInfo info) {

        return toAjax(iEvaluateService.insert(info));
    }

    /**
     * 修改评估
     */
    @ApiOperation("修改评估")
    @SaCheckPermission("sacw:evaluate:edit")
    @Log(title = "评估", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    @Transactional(rollbackFor = Exception.class)
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody EvaluateInfo info) {

        return toAjax(iEvaluateService.update(info));
    }

    /**
     * 删除评估
     */
    @ApiOperation("删除评估")
    @SaCheckPermission("sacw:evaluate:remove")
    @Log(title = "评估", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    @Transactional(rollbackFor = Exception.class)
    public R<Void> remove(@ApiParam("主键串")
                          @NotEmpty(message = "主键不能为空")
                          @PathVariable String id) {

        return toAjax(iEvaluateService.delete(id));
    }

}
